

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class FileExport {

    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    private File sFile, tPath, sWorkPath;

    public FileExport(File file, File path, File workpath) {
        this.sFile = file;
        this.tPath = path;
        this.sWorkPath = workpath;
    }

    public boolean copyFile() {
        try {
            Workbook workbook = null;
            InputStream inputStream = new FileInputStream(sFile.getAbsolutePath());
            if (sFile.getName().endsWith(EXCEL_XLS)) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (sFile.getName().endsWith(EXCEL_XLSX)) {
                workbook = new XSSFWorkbook(inputStream);
            }
            //Excel的sheet数
            int sheet_size = workbook.getNumberOfSheets();
            //目前只写在第一个sheet，固不需要进行循环处理
            if (!(sheet_size > 0)) {

                return false;
            }
            Sheet sheet = workbook.getSheetAt(0);
            int row_num = sheet.getLastRowNum();
            if (!(row_num > 0)) {
                return false;
            }
            for (int i = 1; i <= row_num; i++) {//其他判断待完善，目前默认第一行是名称，第3列是path
                Row row = sheet.getRow(i);
                String mFilePath = "";
                if (sWorkPath.exists()) {
                    mFilePath = sWorkPath.getPath().concat("\\" + row.getCell(2).getStringCellValue());
                }
                File mFile = new File(mFilePath);
                if (!mFile.isFile()) {
                    System.out.println("第" + i + "行的path有误！path读取为：" + mFile.getAbsolutePath());
                    continue;
                }
                File tFile = new File(tPath.getPath().concat("\\" + row.getCell(2).getStringCellValue()));
                FileUtils.copyFile(mFile, tFile);
                if (!tFile.exists()) {
                    System.out.println("第" + i + "行的复制有误！复制文件为：" + mFile.getAbsolutePath());
                    continue;
                }
                System.out.println("第" + i + "行的复制成功！复制文件为：" + tFile.getAbsolutePath());
            }
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
        return true;
    }


    public File getsFile() {
        return sFile;
    }

    public void setsFile(File sFile) {
        this.sFile = sFile;
    }

    public File gettPath() {
        return tPath;
    }

    public void settPath(File tPath) {
        this.tPath = tPath;
    }

    public File getsWorkPath() {
        return sWorkPath;
    }

    public void setsWorkPath(File sWorkPath) {
        this.sWorkPath = sWorkPath;
    }
}
